package it.trick.admin.service;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import it.trick.admin.App;
import it.trick.admin.Constants;
import it.trick.admin.Util;
import it.trick.admin.model.ContentsBaseModel;
import it.trick.admin.model.PageModel;
import java.net.URL;

public class PageService {

    /**
     * ページリストの取得
     * @param chapter
     * @return
     * @throws Exception
     */
    public static List<PageModel> getList(String chapter) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_PAGE_LIST_PATH + chapter);
        Type collectionType = new TypeToken<Collection<PageModel>>() {
        }.getType();

        Gson gson = new Gson();

        List<PageModel> modelList = gson.fromJson(json, collectionType);

        return modelList;
    }

    /**
     * 追加
     * @param model
     * @return
     * @throws Exception
     */
    public static PageModel addPage(ContentsBaseModel model) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.ADD_PAGE_PATH);
        Type responseModelType = new TypeToken<PageModel>() {}.getType();

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(model.getPrarentKeyString());
        psb.append("&");
        psb.append("name=");
        psb.append(model.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(model.getIndex());

        return (PageModel)Http.postGetResponseJson(postURL, psb.toString(), responseModelType);
    }

    /**
     * ページの取得
     * @return
     * @throws Exception
     */
    public static PageModel getPage(String pageKeyString) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_PAGE_PATH + pageKeyString);
        Type type = new TypeToken<PageModel>() {}.getType();

        Gson gson = new Gson();

        PageModel model = gson.fromJson(json, type);

        return model;
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void updatePage (
            PageModel pageModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.UPDATE_PAGE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(pageModel.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(pageModel.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(pageModel.getIndex());
        psb.append("&");
        psb.append("content=");
        psb.append(pageModel.getContent());
        psb.append("&");
        psb.append("keyWord=");
        psb.append(Util.keywordListToString(pageModel.getKeyWordList()));
        psb.append("&");
        psb.append("description=");
        psb.append(pageModel.getDescription());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void changePageIndex (
            PageModel pageModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.CHANGE_PAGE_INDEX_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(pageModel.getKeyValueString());
        psb.append("&");
        psb.append("index=");
        psb.append(pageModel.getIndex());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void changePageName (
            PageModel pageModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.CHANGE_PAGE_NAME_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(pageModel.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(pageModel.getName());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 削除
     * @param categoryKeyString
     */
    public static void deletePage(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.DELETE_PAGE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 公開・非公開
     * @param categoryKeyString
     */
    public static void publicPage(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.PUBLIC_PAGE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }
}
